define(['jquery', 'bootstrap', 'backend', 'table', 'form','template'], function ($, undefined, Backend, Table, Form,Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index' + location.search,
                    add_url: 'goods/goods/add',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    import_url: 'goods/goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");
            
             // 监听下拉列表改变的事件
            $(document).on('change', 'select[name=cate_pid]', function () {
                $("input[name='cate_pid']").val($(this).val());
            });
            
            
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {
                            field: 'cate_pid', title: __('商品分类'), searchList: function (column) {
                                return Template('categorytpl', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'num', title: __('Num')},
                        // {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'basicStr.gg', title: __('规格名称'),operate:false,cellStyle: {css: {"max-width": "100px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}},
                        {field: 'basicStr.intro', title: __('价格'),operate:false,cellStyle: {css: {"max-width": "10px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}},
                        {field: 'basicStr.vips', title: __('会员价'),operate:false,cellStyle: {css: {"max-width": "10px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}},
                        {field: 'basicStr.stock', title: __('库存'),operate:false,cellStyle: {css: {"max-width": "10px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}},
                        {field: 'basicStr.integral', title: __('积分'),operate:false,cellStyle: {css: {"max-width": "10px","white-space":"pre-line","word-wrap":"break-word","word-break":"break-all"}}},

                        // {field: 'basicStr', title: __('规格'),
                        //     formatter : function(value, row){
                        //         return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space:nowrap' title='" + row.basicStr + "'>" + value + "</span>";
                        //     },
                        //     cellStyle : function() {
                        //         return {
                        //             css: {
                        //                 "white-space"   : "nowrap",
                        //                 "text-overflow" : "ellipsis",
                        //                 "overflow"      : "hidden",
                        //                 "max-width"     : "300px",
                        //                 "cursor"        : "pointer"
                        //             }
                        //         };
                        //     }
                        // },
                        {field: 'status', title: __('Status'), searchList: {"1": __('是'), "2": __('否')}, formatter: Table.api.formatter.toggle},
                        // {field: 'status', title: __('Status'),searchList: {1: __('是'), 2: __('否')}, formatter: Table.api.formatter.toggle},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    console.log(params);
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    // filter.cate_pid = 0;
                    // filter.cate_pid = Fast.api.query('cate_pid');
                    // op.cate_pid = ">";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
            });


            //自定义Tab筛选条件
            // $('.panel-heading .nav-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
            //     var value = $(this).data("value");
            //     var options = table.bootstrapTable('getOptions');
            //     var queryParams = options.queryParams;
            //     options.pageNumber = 1;
            //     options.queryParams = function (params) {
            //         //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
            //         params = queryParams(params);
                    
            //         console.log(params);
            //         //如果希望追加搜索条件,可使用
            //         var filter = params.filter ? JSON.parse(params.filter) : {};
            //         var op = params.op ? JSON.parse(params.op) : {};
            //         if (value) {
            //             //这里可以自定义多个筛选条件
            //             filter.admin_id = value;
            //             op.cate_pid = '=';
            //         } else {
            //             //选全部时要移除相应的条件
            //             delete filter.cate_pid;
            //             delete op.cate_pid;
            //         }

            //         params.filter = JSON.stringify(filter);
            //         params.op = JSON.stringify(op);

            //         //如果希望忽略搜索栏搜索条件,可使用
            //         //params.filter = JSON.stringify(value?{admin_id: value}:{});
            //         //params.op = JSON.stringify(value?{admin_id: '='}:{});
            //         return params;
            //     };

            //     table.trigger("uncheckbox");
            //     table.bootstrapTable('refresh', {pageNumber: 1});
            //     return false;
            // });
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
